//
//  SaoImgController.m
//  CreateMyCode
//
//  Created by zhibao on 2024/12/17.
//

#import "SaoIngController.h"
#import <Photos/Photos.h>
#import "ZHBarTool.h"
#import "ZHBarSacnView.h"
#import "SaoFinishController.h"
@interface SaoIngController ()<ZHBarDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@end

@implementation SaoIngController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.gk_navBackgroundColor = [UIColor clearColor];
    self.gk_navTitle = LocalizedString(@"二维码及条码识别");
    self.gk_navTitleColor = [UIColor whiteColor];
    self.gk_navBarAlpha = 0.0;
    self.gk_backImage = [UIImage imageNamed:@"arrow_back_white"];
    [self hideNavLine];
    
    ZHBarSacnView *scanview = [ZHBarSacnView barSacnViewWithFrame:CGRectMake(0, 0, MY_Screen_W, MY_Screen_H) scanRect:CGRectMake((MY_Screen_W - 300) / 2 , (MY_Screen_H - NAVIH - 300) / 2 , 300, 300)];
    [scanview setChoosePicture:^(UIButton *sender) {
        [self choosePic];
    }];
    [self.view addSubview:scanview];
    
    [ZHBarTool barTool].delegate = self;
    [[ZHBarTool barTool] startScanBarWithScanRect:scanview.scanRect scanType:ZHBarScanTypeAll layerView:scanview];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[ZHBarTool barTool] startScanning];
    self.gk_statusBarStyle = UIStatusBarStyleLightContent;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.gk_statusBarStyle = UIStatusBarStyleDefault;
}

- (void)choosePic {
    PHAuthorizationStatus authStatus = [PHPhotoLibrary authorizationStatus];
    if (authStatus == PHAuthorizationStatusNotDetermined) {
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            DLog(@"status==%ld",(long)status);
            if (status == PHAuthorizationStatusAuthorized || status == PHAuthorizationStatusAuthorized) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
                    pickerController.delegate = self;
                    pickerController.editing = YES;
                    pickerController.delegate = self;
                    pickerController.allowsEditing = YES;
                    //    去除毛玻璃效果
                    pickerController.navigationBar.translucent = NO;
                    pickerController.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
                    [self presentViewController:pickerController animated:YES completion:nil];
                });
                
            }else{
                NSAssert(status == PHAuthorizationStatusAuthorized, LocalizedString(@"无相册权限，请到“设置”中开启相册权限"));
            }
        }];
    }else{
        UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
        pickerController.delegate = self;
        pickerController.editing = YES;
        pickerController.delegate = self;
        pickerController.allowsEditing = YES;
        //    去除毛玻璃效果
        pickerController.navigationBar.translucent = NO;
        pickerController.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:pickerController animated:YES completion:nil];
    }
    
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    NSString *content = [ZHBarTool decodeQRCodeImage:image];
    [self handleScanObject:content type:ZHBarScanTypeQRCode];
}

- (void)ZHBarAuthorizedCameraFailed {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{}completionHandler:nil];
}
-(void)ZHBarDidScanBarGetObject:(NSString *)object barType:(ZHBarScanType)barType {
    [self handleScanObject:object type:barType];
}

- (void)handleScanObject:(NSString *)object type:(ZHBarScanType)type {
    [[ZHBarTool barTool] stopScanning];
    if ([object isKindOfClass:[NSString class]] && object.length) {
        if ([object containsString:@"://"]) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:object] options:@{} completionHandler:nil];
            [[ZHBarTool barTool] stopScanning];
        } else {
            NSLog(@"扫描结果=%@", object);
            [[ZHBarTool barTool] stopScanning];
            SaoFinishController *controller = [SaoFinishController new];
            controller.content = object;
            [self.navigationController pushViewController:controller animated:YES];
        }
    }
}

@end
